package com.iOS.service.impl;

import com.iOS.dao.NoteMapper;
import com.iOS.dto.NoteExecution;
import com.iOS.entity.Note;
import com.iOS.enums.NoteEnum;
import com.iOS.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteMapper noteMapper;
    @Override
    public NoteExecution addNote(Note note) {
        try {
            Note newNote = new Note();
            newNote.setUserId(note.getUserId());
            newNote.setNoteContext(note.getNoteContext());
            newNote.setNoteTitle(note.getNoteTitle());
            newNote.setNotePictures(note.getNotePictures());
            newNote.setCreateTime(new Date());
            if(noteMapper.insert(newNote)==1) {
                return new NoteExecution(NoteEnum.SUCCESS);
            }
        } catch (Exception e) {
            return new NoteExecution(NoteEnum.INNER_ERROR);
        }
        return new NoteExecution(NoteEnum.FAIL);
    }

    @Override
    public NoteExecution deleteNoteById(int Id) {
        try{
            if(noteMapper.deleteById(Id)==1)
            {
                return new NoteExecution(NoteEnum.SUCCESS);
            }
        }catch (Exception e) {
            return new NoteExecution(NoteEnum.INNER_ERROR);
        }
        return new NoteExecution(NoteEnum.FAIL);

    }

    @Override
    public NoteExecution updateNoteById(Note note) {
        try {
            Note N= new Note();
            N.setId(note.getId());
            N.setUserId(note.getUserId());
            N.setNoteContext(note.getNoteContext());
            N.setNoteTitle(note.getNoteTitle());
            N.setNotePictures(note.getNotePictures());
            N.setCreateTime(new Date());
            int a=noteMapper.updateById(N);
            if(a==1)
                return new NoteExecution(NoteEnum.SUCCESS,N);
            else
                return new NoteExecution(NoteEnum.FAIL);
        } catch (Exception e) {
            return new NoteExecution(NoteEnum.INNER_ERROR);
        }
    }

    @Override
    public NoteExecution queryAllNote(int userId) {
        try {
            List<Note> notes=noteMapper.queryAllNote(userId);
            if(notes!=null)
                return new NoteExecution(NoteEnum.SUCCESS,notes);
            else
                return new NoteExecution(NoteEnum.FAIL);
        } catch (Exception e) {
            return new NoteExecution(NoteEnum.INNER_ERROR);
        }
    }
}
